package com.android.execucaocampo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.commons.net.io.Util;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.android.beans.Imovel;
import com.android.beans.OrdemServico;
import com.android.data.RepositorioImovel;
import com.android.data.RepositorioOrdemServico;
import com.android.execucaocampo.R;

public class ListarDatasActivity extends Activity {

	public ListView lista;

	ArrayList<java.sql.Date> arrayDatas;
	  SimpleDateFormat out = new SimpleDateFormat("dd/MM/yyyy");

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listadatas);
		
		RepositorioImovel repImovel = new RepositorioImovel(getApplicationContext());
		RepositorioOrdemServico rep = new RepositorioOrdemServico(getApplicationContext());


		arrayDatas = rep.listarDatasDisponivel(" WHERE codigoequipe = '" + PrincipalActivity.codigoEquipe + "'");
		 
		ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
		HashMap<String, String> map;
						
		for (java.sql.Date datas : arrayDatas) {
			map = new HashMap<String, String>();
			map.put("data", out.format(datas));
			mylist.add(map);
		}
				
		lista = (ListView) findViewById(R.id.listaDatas);
		
		lista.setAdapter(new OtherSimpleAdapter(this, mylist, R.layout.rowdata,
            new String[] {"data"}, 
            new int[] {R.id.data}));
		
		// ao clicar em um item da lista
		lista.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {

				HashMap<String, String> map = (HashMap<String, String>) lista.getItemAtPosition(arg2);
				
				Bundle b = new Bundle();
				
				b.putString("data", map.get("data"));
				
				abrirEditar(b);				
			}

		});
		
		Button btVoltar = (Button) findViewById(R.botao.voltar);
		btVoltar.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				Bundle b = new Bundle();
				
				b.putString("equipe", PrincipalActivity.usuario);
				b.putString("codigo", PrincipalActivity.codigoEquipe);				

				// Navegar entre as telas				
				Intent i = new Intent(ListarDatasActivity.this,PrincipalActivity.class);
				//Intent i = new Intent(this,SlideScreenActivity.class);
		    	finish();
		    	i.putExtras(b);
		    	startActivity(i);
			}
		});
	}
	
	public void abrirEditar(Bundle extras){
    	Intent i = new Intent(this,ListaAtividadesActivity.class);
		//Intent i = new Intent(this,SlideScreenActivity.class);
    	finish();
    	i.putExtras(extras);
    	startActivity(i);
    }
}
